<?php
//============================================================+
require_once('./tcpdf/config/lang/ron.php');
require_once('./tcpdf/tcpdf.php');


define("DEFAULT_MAX_TEXT_LINE_LENGTH",     55);

// extend TCPF with custom functions
class MYPDF extends TCPDF {

	// Load table data from file
	public function LoadData($header) {
		// Read file lines
		$data = array();
		
		if ( isset($_POST['number_of_rows']) and  !empty($_POST['number_of_rows']) )
		{		
			for ($rowIndex = 0; $rowIndex < intval($_POST['number_of_rows']); $rowIndex++ )
			{
				
				$table_prefix = "tabel_".strval($rowIndex + 1 )."_";
				
				$data[$rowIndex][0] = strval($rowIndex + 1);
				
				if ( isset($_POST[$table_prefix.'cod_produs']) and  !empty($_POST[$table_prefix.'cod_produs']) )
					$data[$rowIndex][1] = $_POST[$table_prefix.'cod_produs'];
				else
					$data[$rowIndex][1] = "invalid";;
					
				if ( isset($_POST[$table_prefix.'denumire_material']) and  !empty($_POST[$table_prefix.'denumire_material']) )
					$data[$rowIndex][2] = $_POST[$table_prefix.'denumire_material'];
				else
					$data[$rowIndex][2] = "invalid";;
					
				if ( isset($_POST[$table_prefix.'unitate_masura']) and  !empty($_POST[$table_prefix.'unitate_masura']) )
					$data[$rowIndex][3] = $_POST[$table_prefix.'unitate_masura'];
				else
					$data[$rowIndex][3] = "invalid";;	
									
				if ( isset($_POST[$table_prefix.'cantitate']) and  !empty($_POST[$table_prefix.'cantitate']) )
					if ( preg_match('/^\d+(\.\d+)?$/',$_POST[$table_prefix.'cantitate']) != 0 )	
						$data[$rowIndex][4] = $_POST[$table_prefix.'cantitate'];
					else
						$data[$rowIndex][4] = "invalid";
				else
					$data[$rowIndex][4] = "invalid";

				if ( isset($_POST[$table_prefix.'pret_um']) and  !empty($_POST[$table_prefix.'pret_um']) )
					if ( preg_match('/^\d+(\.\d+)?$/',$_POST[$table_prefix.'pret_um']) != 0 )	
						$data[$rowIndex][5] = $_POST[$table_prefix.'pret_um'];
					else
						$data[$rowIndex][5] = "invalid";	
				else
					$data[$rowIndex][5] = "invalid";


				if( $data[$rowIndex][4] != "invalid" && $data[$rowIndex][5] != "invalid" )
						$data[$rowIndex][6] = floatval($data[$rowIndex][4]) * floatval($data[$rowIndex][5]);
				else
						$data[$rowIndex][6] = "invalid";
					
				
			}		
		}
		
		
		
		return $data;
	}

	// Colored table
	public function ColoredTable($header,$data) {
		// Colors, line width and bold font
		$this->SetFillColor(255, 255, 255);
		$this->SetTextColor(0);
		$this->SetDrawColor(0, 0, 0);
		$this->SetLineWidth(0.4);
		
		$this->SetFont('helvetica', '', 11);
		// Header
		$w = array(20, 25, 40, 25, 25, 25, 25);
		$num_headers = count($header);
		for($i = 0; $i < $num_headers; ++$i) {
			$this->MultiCell($w[$i], 10, $header[$i], 1, 'C', 1, 0, '', '', true, 0, false, true, 0 , 'M');
		}
		$this->Ln();
		// Color and font restoration
		$this->SetTextColor(0);
		$this->SetFont('helvetica', '', 11);
		// Data
		
		$valoare_totala = 0;
		
		foreach($data as $row) {
			
			for ( $columnIndex = 0; $columnIndex < count($header); $columnIndex++ )
			{
					if( $row[$columnIndex] == "invalid" )
						$this->SetTextColor(255,0,0);			
					else
						$this->SetTextColor(0,0,0);

					if($columnIndex == ( count($header) - 1) )
					{
						if( $row[$columnIndex] != "invalid" )
						{
							$valoare_totala += floatval($row[$columnIndex]);	
						}
					}

					$this->Cell($w[$columnIndex], 6, $row[$columnIndex], 1, 0, 'C',0);
			}
			
			$this->Ln();
		}
		
		$this->SetTextColor(0,0,0);
		
		$this->SetX(150);
		$this->Cell(25, 6, 'Total', 0, 0, 'L', 0);
		$this->Cell(25, 6, $valoare_totala, 1, 0, 'C', 0);
		$this->Ln();
		$this->SetX(150);
		$this->Cell(25, 6, 'DISCOUNT', 0, 0, 'L', 0);
		$this->Cell(25, 6, '-', 1, 0, 'C', 0);
		$this->Ln();
		$this->SetX(150);
		$this->Cell(25, 6, 'TVA 24%', 0, 0, 'L', 0);
		$this->Cell(25, 6, $valoare_totala * 0.24 , 1, 0, 'C', 0);
	}
	
	    //Page header
    public function Header() {
        // Logo
        $image_file = 'images'.DIRECTORY_SEPARATOR.'generate_pdf'.DIRECTORY_SEPARATOR.'antet1.jpg';
        $this->Image($image_file, '', '', '', '', 'JPG', '', 'T', false, 300, 'M', false, false, 'C', false, false, 'B');
        // Set font
		
		$this->SetY(45);
        $this->SetFont('times', '', 10);
        $this->Cell(100, 15, 'CNG/BUC/VITAN/PC/08', 0, false, 'L', 0, '', 0, false, 'M', 'M');
		
		$this->SetY(50);
        $this->SetFont('times', '', 10);
        $this->Cell(100, 15, '4/4/2008', 0, false, 'L', 0, '', 0, false, 'M', 'M');
		
    }

    // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
        $this->Cell(0, 10, 'Pagina '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }
	
}

// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Bogdan');
$pdf->SetTitle('Table Example');
$pdf->SetSubject('Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
//$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 011', PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, 'Change Header', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, 'Change Footer', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// set font
$pdf->SetFont('helvetica', '', 12);

// add a page
$pdf->AddPage();



if ( isset($_POST['catre']) and  !empty($_POST['catre']) )
	$pdf->SetTextColor(0,0,0);
else
	$pdf->SetTextColor(255,0,0);
$pdf->SetY(60);
$pdf->SetFont('times', 'B', 14);
$pdf->Cell(70, 15, 'Catre : ', 0, false, 'R', 0, '', 0, false, 'M', 'M');
$pdf->SetFont('times', '', 14);
if ( isset($_POST['catre']) and  !empty($_POST['catre']) )
{	
	$pdf->Cell(70, 15, $_POST['catre'], 0, false, 'L', 0, '', 0, false, 'M', 'M');
}


if ( isset($_POST['fax']) and  !empty($_POST['fax']) )
	$pdf->SetTextColor(0,0,0);
else
	$pdf->SetTextColor(255,0,0);
$pdf->SetY(65);
$pdf->SetFont('times', '', 10);
$pdf->Cell(70, 15, 'FAX : ', 0, false, 'R', 0, '', 0, false, 'M', 'M');	
if ( isset($_POST['fax']) and  !empty($_POST['fax']) )
{	
	$pdf->Cell(70, 15, $_POST['fax'], 0, false, 'L', 0, '', 0, false, 'M', 'M');
}

if ( isset($_POST['in_atentia']) and  !empty($_POST['in_atentia']) )
	$pdf->SetTextColor(0,0,0);
else
	$pdf->SetTextColor(255,0,0);
$pdf->SetY(70);
$pdf->Cell(70, 15, 'IN ATENTIA:', 0, false, 'R', 0, '', 0, false, 'M', 'M');	
if ( isset($_POST['in_atentia']) and  !empty($_POST['in_atentia']) )
{	
	$pdf->Cell(70, 15, $_POST['in_atentia'], 0, false, 'L', 0, '', 0, false, 'M', 'M');
}

if ( isset($_POST['nr_comanda']) and  !empty($_POST['nr_comanda']) )
	$pdf->SetTextColor(0,0,0);
else
	$pdf->SetTextColor(255,0,0);
$pdf->SetY(80);
$pdf->SetFont('times', 'U', 16);
$pdf->Cell(120, 15, 'COMANDA:', 0, false, 'C', 0, '', 0, false, 'M', 'M');
if ( isset($_POST['nr_comanda']) and  !empty($_POST['nr_comanda']) )
{	
	$pdf->Cell(50, 15, $_POST['nr_comanda'], 0, false, 'L', 0, '', 0, false, 'M', 'M');
}

$pdf->SetTextColor(0,0,0);

//Column titles
$header = array('Nr.Crt', 'Cod Produs', 'Denumire material', 'UM', 'Cantitate', 'PRET / UM','Total');

//Data loading
$data = $pdf->LoadData($header);

$pdf->SetY(100);

// print colored table
$pdf->ColoredTable($header, $data);

$y_position = 150;


$form_details =  array(
	'Pret TOTAL' => '',
	'Conditii de livrare' => '',
    'Termen de livrare' => '',
	'Montaj' => '',
	'Facturare' => '',
	'Termene si modalitati de plata' => '',
	'Penalitati' => '',
	'Garantie' => '',
	'Marfa va fi insotita de' => '',
	'Transportul' => '',
);

if ( isset($_POST['pret_total']) and  !empty($_POST['pret_total']) )
{	
		$form_details['Pret TOTAL'] = $_POST['pret_total'];
}
if ( isset($_POST['moneda']) and  !empty($_POST['moneda']) )
{	
		$form_details['Pret TOTAL'] = $form_details['Pret TOTAL'].$_POST['moneda'];
}
if ( isset($_POST['conditii_livrare']) and  !empty($_POST['conditii_livrare']) )
{	
		$form_details['Conditii de livrare'] = $_POST['conditii_livrare'];
}
if ( isset($_POST['termen_livrare']) and  !empty($_POST['termen_livrare']) )
{	
		$form_details['Termen de livrare'] = $_POST['termen_livrare'];
}
if ( isset($_POST['montaj']) and  !empty($_POST['montaj']) )
{	
		$form_details['Montaj'] = $_POST['montaj'];
}
if ( isset($_POST['facturare']) and  !empty($_POST['facturare']) )
{	
		$form_details['Facturare'] = $_POST['facturare'];
}
if ( isset($_POST['termene']) and  !empty($_POST['termene']) )
{	
		$form_details['Termene si modalitati de plata'] = $_POST['termene'];
}
if ( isset($_POST['penalitati']) and  !empty($_POST['penalitati']) )
{	
		$form_details['Penalitati'] = $_POST['penalitati'];
}
if ( isset($_POST['garantie']) and  !empty($_POST['garantie']) )
{	
		$form_details['Garantie'] = $_POST['garantie'];
}
if ( isset($_POST['marfa']) and  !empty($_POST['marfa']) )
{	
		$form_details['Marfa va fi insotita de'] = $_POST['marfa'];
}
if ( isset($_POST['transport']) and  !empty($_POST['transport']) )
{	
		$form_details['Transportul'] = $_POST['transport'];
}


$pdf->SetY($y_position);
foreach ($form_details as $key => $value)
{	
	if ( strlen($key) > strlen($value))
	{
		$max_length = strlen($key);	
	}
	else
	{
		$max_length = strlen($value);	
	}
	
	
	if(empty($value) or $value == 'neselectat')
	{
		$pdf->SetTextColor(255,0,0);
	}
	else
	{
		$pdf->SetTextColor(0,0,0);
	}
	
	$y_position += 5 * ((int)( $max_length / 43) + 1 );
	$pdf->SetFont('times', 'B', 12);
	$pdf->MultiCell(100, 10, $key.':   ', 0, 'L', false, 0, '', '', true );
	$pdf->SetFont('times', '', 12);
	$pdf->MultiCell(100, 10, $value.';', 0, 'L', false, 0, '', '', true );
	$pdf->SetY($y_position);
}



$pdf->SetTextColor(0,0,0);
$y_position += 20;
$pdf->SetY($y_position);
$pdf->setCellPaddings(2, 4, 6, 8);
$pdf->SetFont('times', '', 10);
$pdf->MultiCell(40, 10, 'Cristian Patrascu ', 0, 'L', false, 0, '', '', true );
$pdf->SetX(150);
$pdf->SetFont('times', '', 10);
$pdf->MultiCell(40, 10, 'Director General, ing. Ion STAN', 0, 'L', false, 0, '', '', true );

//Close and output PDF document
$pdf->Output('example_011.pdf', 'I');

//============================================================+
// END OF FILE                                                
//============================================================+

